package com.netrust.quarz.hw.token;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.netrust.quarz.hw.HttpsClientUtil;
import com.netrust.quarz.hw.NodeConfig;
import com.netrust.quarz.hw.http.RequestType;
import com.netrust.quarz.hw.token.module.DxHlwMo;
import com.netrust.quarz.hw.token.module.DxZwwwMo;
import com.netrust.quarz.hw.token.module.OcMo;
import com.netrust.quarz.hw.token.module.YdHlwMo;
import com.netrust.quarz.hw.token.module.YdZwwwMo;

import net.sf.json.JSONObject;

/**
* @title 
* @description
* @author   ZYC
* @version  v_1.0.0
* @date   2017年10月14日 下午3:19:29
*/
@Component
public class OcUtils {
	@Autowired
	DxZwwwMo dxZwwwMo;
	@Autowired
	YdZwwwMo ydZwwwMo;
	@Autowired
	DxHlwMo dxHlwMo;
	@Autowired
	YdHlwMo ydHlwMo;
	
	private OcMo mo;
	
	
	
	public OcMo getMo() {
		return mo;
	}

	public void setMo(String key) {
		switch (key) {
			case "电信政务外网":
				mo=dxZwwwMo;
				break;
			case "电信互联网":
				mo=dxHlwMo;
				break;
			case "移动政务外网":
				mo=ydZwwwMo;
				break;
			case "移动互联网":
				mo=ydHlwMo;
				break;
		}
	}

	public String token() throws Exception{
		JSONObject js = new JSONObject();
		js.put("user_id",mo.getUsername());
		js.put("value", mo.getPassword());
		js.put("host_ip",mo.getHostIp());

		try {
			Object tokenData = new HttpsClientUtil().sendRequest("/oc/v2.3/tokens", RequestType.POST,js.toString(),nodeConfig());
		        
	        if (tokenData instanceof String){
	            return String.valueOf(tokenData);
	        }else{
	            return null;
	        }
		} catch (Exception e) {
			throw e;
		}
	}
	
	
	public NodeConfig nodeConfig() {
		NodeConfig nodeConf = new NodeConfig();
        nodeConf.setOCIp(mo.getOcIp()); //电信党政2.37.106.234
        nodeConf.setPort(mo.getPort());//OC的访问IP，默认63234 我们的31943
        HashMap<String, String> certificate = new HashMap<String, String>();
        certificate.put("keyStoreName", mo.getKeyStoreName());
        certificate.put("keyStorePwd", mo.getKeyStorePwd()); //证书库密钥，生成证书时指定
        certificate.put("trustStoreName",mo.getTrustStoreName());
        certificate.put("trustStorePwd",mo.getTrustStorePwd());//证书库密钥，生成证书时指定
        nodeConf.setCertificate(certificate);
        nodeConf.setHostIP(mo.getHostIp());//本机IP地址3.3.3.6
        return nodeConf;
	}
}
